package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import model.Document;

public class DocumentDAOImpl implements DocumentDAO{
	
	 private DataSource dataSource;
	 
	    public void setDataSource(DataSource dataSource) {
	        this.dataSource = dataSource;
	    }
	    
	    
	@Override
	public void save(Document document) {
		// TODO Auto-generated method stub
		String query = "insert into document(DocId, DocName, TypeId, OriginalDate, NotarizedDate, Description, Status,NumberOfDownload,FilePath) value(?,?,?,?,?,?,?,?,?)";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, document.getDocId());
            ps.setString(2, document.getDocName());
            ps.setInt(3, document.getTypeId());
            ps.setString(4, document.getOriginalDate());
            ps.setString(5, document.getNotarizedDate());
            ps.setString(6, document.getDescription());
            ps.setString(7, document.getStatus());
            ps.setInt(8, document.getNumberOfDownload());
            ps.setString(9, document.getFilePath());
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("document saved with id="+document.getDocId());
            }else System.out.println("document save failed with id="+document.getDocId());
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
	}

	@Override
	public Document getById(int docId) {
		// TODO Auto-generated method stub
		String query = "select * from document where DocId =?";
        Document doc = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, docId);
            rs = ps.executeQuery();
            if(rs.next()){
                doc = new Document();
                doc.setDocId(rs.getInt("DocId"));
                System.out.println("DOc Id = " + docId + rs.getInt("DocId"));
//                (`DocId`, `DocName`, `TypeId`, `OriginalDate`, `NotarizedDate`, `Description`, `Status`)
                doc.setDocName(rs.getString("DocName"));
                doc.setTypeId(rs.getInt("TypeId"));
                doc.setOriginalDate(rs.getString("OriginalDate"));
                doc.setNotarizedDate(rs.getString("NotarizedDate"));
                doc.setDescription(rs.getString("Description"));;
                doc.setStatus(rs.getString("Status"));
                doc.setNumberOfDownload(rs.getInt("NumberOfDownload"));
                doc.setFilePath(rs.getString("FilePath"));
                System.out.println(" Found::"+doc);
            }else{
                System.out.println("Nofound with id="+docId);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		return doc;
	}

	@Override
	public void update(Document document) {
		// TODO Auto-generated method stub
		String query = "update document set DocName=?, TypeId=?, OriginalDate=?, NotarizedDate=?, Description=?, Status=?, NumberOfDownload=?, FilePath=? where DocId=?";
		Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setString(1, document.getDocName());
            ps.setInt(2, document.getTypeId());
            ps.setString(3, document.getOriginalDate());
            ps.setString(4, document.getNotarizedDate());
            ps.setString(5, document.getDescription());
            ps.setString(6, document.getStatus());
            ps.setInt(7, document.getNumberOfDownload());
            ps.setString(8, document.getFilePath());
            ps.setInt(9, document.getDocId());
            
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("updated with id="+document.getDocId());
            }else System.out.println("No found with id="+document.getDocId());
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
	}

	@Override
	public void deleteById(int docId) {
		// TODO Auto-generated method stub
		String query = "delete from document where DocId=?";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, docId);
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println(" deleted with id="+docId);
            }else System.out.println("No  found with id="+docId);
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
	}

	@Override
	public List<Document> getAll() {
		// TODO Auto-generated method stub
		String query = "select * from document";
		List<Document> list = new ArrayList<Document>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();
            while(rs.next()){
                Document doc = new Document();
                doc.setDocId(rs.getInt("DocId"));
              doc.setDocName(rs.getString("DocName"));
              doc.setTypeId(rs.getInt("TypeId"));
              doc.setOriginalDate(rs.getString("OriginalDate"));
              doc.setNotarizedDate(rs.getString("NotarizedDate"));
              doc.setDescription(rs.getString("Description"));;
              doc.setStatus(rs.getString("Status"));
              doc.setNumberOfDownload(rs.getInt("NumberOfDownload"));
              doc.setFilePath(rs.getString("FilePath"));
              
                list.add(doc);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		return list;
	}
	
}
